package com.jshoperxms.service.impl;

import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.maths.Arith;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.dao.MemberRechargeRdTDao;
import com.jshoperxms.dao.MemberRechargeTDao;
import com.jshoperxms.entity.MemberRechargeRdT;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.jshoperxms.entity.MemberRechargeT;
import com.jshoperxms.service.MemberRechargeTService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service("memberRechargeTService")
@Scope("prototype")
public class MemberRechargeTServiceImpl extends BaseTServiceImpl<MemberRechargeT> implements MemberRechargeTService {


    @Resource
    private MemberRechargeRdTDao memberRechargeRdTDao;

    @Resource
    private MemberRechargeTDao memberRechargeTDao;

    @Resource
    private Serial serial;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void updateMemberRechargeTransactional(MemberRechargeT rq,String type) {

        Criterion criterion= Restrictions.eq("memberid",rq.getMemberid());
        MemberRechargeT bean=this.memberRechargeTDao.findOneByCriteria(MemberRechargeT.class,criterion);
        if(bean!=null){
           // bean.setBalance(rq.getBalance());
            bean.setBalance(BigDecimal.valueOf(Arith.add(bean.getBalance().doubleValue(),rq.getBalance().doubleValue())));
            bean.setUpdatetime(DateTools.getSystemTimeBi());
            bean.setVersiont(bean.getVersiont()+1);
            this.memberRechargeTDao.update(bean);
            saveMemberRechargeRd(rq,type);
        }else{
            bean=new MemberRechargeT();
            bean.setId(this.serial.Serialid(SerialKeys.MEMBERRECHARGET));
            bean.setBalance(rq.getBalance());
            bean.setCreatetime(DateTools.getSystemTimeBi());
            bean.setMembername(rq.getMembername());
            bean.setMemberid(rq.getMemberid());
            bean.setUpdatetime(DateTools.getSystemTimeBi());
            bean.setStatus(BaseEnums.DataUsingState.USING.getState());
            bean.setVersiont(1);
            this.memberRechargeTDao.save(bean);
            saveMemberRechargeRd(rq,type);
        }



    }


    /**
     * 保存充值记录
     * @param rq
     * @param type
     */
    private void saveMemberRechargeRd(MemberRechargeT rq,String type){
        // type 1，支付宝，2,微信，3网银，4系统代充
        MemberRechargeRdT memberRechargeRdT=new MemberRechargeRdT();
        memberRechargeRdT.setId(this.serial.Serialid(SerialKeys.MEMBERRECHARGETRECORDS));
        memberRechargeRdT.setCreatetime(DateTools.getSystemTimeBi());
        memberRechargeRdT.setMemberid(rq.getMemberid());
        memberRechargeRdT.setMembername(rq.getMembername());
        memberRechargeRdT.setMoney(rq.getBalance());
        memberRechargeRdT.setUpdatetime(DateTools.getSystemTimeBi());
        memberRechargeRdT.setStatus(BaseEnums.DataUsingState.USING.getState());
        memberRechargeRdT.setType(type);
        this.memberRechargeRdTDao.save(memberRechargeRdT);
    }
}
